import path from 'path'
import fs from 'fs'
import cjs from '@rollup/plugin-commonjs'
import ts from 'rollup-plugin-typescript2'

const pkgPath = path.resolve(__dirname, '../../packages')
const distPath = path.resolve(__dirname, '../../dist/node_modules')

export function resolvePkgPath(pkgName, isDist) {
  if (isDist) {
    return `${distPath}/${pkgName}`
  } else {
    return `${pkgPath}/${pkgName}`
  }
}


/** 解析包 */
export function getPackageJSON(pkgName) {
  // 包路径
  const _path = `${resolvePkgPath(pkgName)}/package.json`
  const pkgStr = fs.readFileSync(_path, { encoding: "utf-8" })
  return JSON.parse(pkgStr)
}



/** 共用的plugins */
export function getPluginis(typescript = {}) {
  return [cjs(), ts(typescript)]
}